import sys
import os

# 添加项目根目录到Python路径
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '../../../')))

from src.local_tools.projectManager import ProjectManager


def save_testcase_file(file_path="test/test_code1.py", content="", mode="w"):
    """
    保存测试代码文件到指定路径
    
    Args:
        file_path: 相对路径文件名，默认是 test/test_code1.py
        content: 文件内容
        mode: 文件写入模式，默认是新建'w'，对应ProjectManager的'overwrite'
            'w' -> 'overwrite'
            'a' -> 'append'
    
    Returns:
        生成的文件的绝对路径
    """
    # 初始化ProjectManager
    manager = ProjectManager()
    
    # 转换模式
    manager_mode = 'overwrite' if mode == 'w' else 'append'
    
    # 调用generate_file保存文件
    return manager.generate_file(file_path, content, mode=manager_mode)


# 示例用法
if __name__ == "__main__":
    example_content = '''import unittest
from src.code1 import process_data, hello_world


class TestCode1(unittest.TestCase):
    def test_process_data(self):
        """测试process_data函数"""
        # 测试用例1: 正常数据
        test_data = [1, 2, 3, 4, 5]
        expected = [2, 4, 6, 8, 10]
        self.assertEqual(process_data(test_data), expected)
        
        # 测试用例2: 空列表
        self.assertEqual(process_data([]), [])
        
        # 测试用例3: 包含负数
        test_data = [-1, -2, 0]
        expected = [-2, -4, 0]
        self.assertEqual(process_data(test_data), expected)
    
    def test_hello_world(self):
        """测试hello_world函数（简单的输出测试）"""
        # 这里主要是确保函数能正常运行，不会抛出异常
        try:
            hello_world()
            success = True
        except Exception:
            success = False
        self.assertTrue(success, "hello_world函数执行失败")


if __name__ == '__main__':
    unittest.main()
'''
    
    file_path = save_testcase_file(content=example_content)
    print(f"测试代码文件已保存至: {file_path}")